`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    15:15:11 12/01/2011 
// Design Name: 
// Module Name:    BallMovement 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module BallMovement(clk, reset, pause, x, y, p1, p2, plyr_input, plyr2_input, p1_score, p2_score, enableSound);

input reset, clk, pause;
input [8:0] p1, p2;	//paddles
output x;
output y;
output reg [3:0] p1_score, p2_score;
output reg enableSound;  

input [11:0] plyr_input, plyr2_input;

reg [9:0] x = 288;
reg [8:0] y = 240;

//output reg p1score,p2score;
reg pausegame;

// Verticel and Horizontal
reg vert = 0; // 0:Up 1:down 
reg hor = 0; // 0: left,

parameter dec = 10000;
reg [31:0] speed = 300000;
reg [31:0] cnt = 0;

// BeginGame, REPEATING FUNCTIONS BELOW

// s0 - direction
// s1 - rightside screen boundaries = 640-64
// s2 - bottom of screen boundaries = 480-32
// s3 - score width from edge of screen?
// s4 - balls speed

// Collision Detection for ball
always@ (posedge clk)
begin
	if (pausegame|reset) begin
		x <=288;
		y <= 240;
		if(reset) begin
		p1_score <= 0;
		p2_score <= 0;
		end
		pausegame <= 1;
		speed <= 300000;
		if (plyr_input[8] | plyr2_input[8]) begin
			pausegame <= 0;
		end
		
	end
	
	else begin
		if(cnt == speed) begin
			cnt <= 0;
				if (pause)
					begin
						x <= x;
						y <= y;
					end
				else begin
						// moving right
						if (hor) begin
								x <= x + 1;
								// check collision
								if (x == 575 & y+31 > (p2) & y < p2+96) begin
										hor <= 1'b0;
										speed <= speed - dec;
										enableSound <= 1;
								end
								else begin enableSound <=0; end
								
								if (x >= 576) begin
									//score
									p1_score <= p1_score + 1;
									pausegame <=1;
								end
						end
						// moving left
						else begin
								x <= x - 1;
								// check collision
								if (x == 32 & y+31 > (p1) & y < p1+96) begin
										hor <= 1'b1;
										speed <= speed - dec;
										enableSound <=1;
								end
								else begin enableSound <=0; end
								
								if ( x <= 1) begin
									//score
									p2_score <= p2_score + 1;
									pausegame <= 1;
								end
							end
							
							
						if (vert) begin //up 
								y <= y + 1;
								// check collision							
								if (y >= 448) begin
										vert <= 1'b0;
									end
							end
						else //down
							begin
								y <= y - 1;
								// check collision							
								if (y <= 1) begin
										vert <= 1'b1;
								end
							end
				end
		end
		else begin// cnt != speed
			 cnt <= cnt+1;
			 //p1score <= 0;
			 //p2score <= 0;
		end
	end
end

endmodule
